package com.lucun.es_transaction.controller;

import com.lucun.es_transaction.config.RolePermission;
import com.lucun.es_transaction.entity.Commodity;
import com.lucun.es_transaction.entity.Payment;
import com.lucun.es_transaction.entity.UserPayment;
import com.lucun.es_transaction.service.CommodityService;
import com.lucun.es_transaction.service.UserPaymentService;
import com.lucun.es_transaction.util.Recommend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: truedei
 * @Date: 2020 /20-6-13 20:19
 * @Description: 推荐系统
 * 直接查看需要什么
 */
@Controller
@RequestMapping("/user")
public class RecommenderS {
    @Autowired
    UserPaymentService userPaymentService;
    @Autowired
    CommodityService commodityService;

    //推荐算法开始

    /**
     * 登录后推荐接口
     *
     * @param userPayment
     * @需要的 userId 模拟登录的用户Id
     */
    @RolePermission(login = false)
    @PostMapping("/recommend")
    @ResponseBody
    public List<Commodity> login(@RequestBody UserPayment userPayment, HttpServletRequest request) {

        //1，使用该用户的名字获取订单信息
        System.out.println("----------------");
        //查询登录用户的订单信息
        List<UserPayment> userPaymentList = userPaymentService.getProductOrderList(userPayment,request);

        //存储个人 购买的所有的商品id
        Long[] ints = new Long[userPaymentList.size()];

        //存储个人信息，封装成对象，方便计算
        Payment userR = new Payment();

        //筛选出来个人订单中的商品的id
        for (int i = 0; i < userPaymentList.size(); i++) {
            ints[i] = Long.valueOf(userPaymentList.get(i).getCommId());
        }
        userR.setUserId(userPaymentList.get(0).getUserId());
        userR.setProductIds(ints);


        //2,拿到所有用户的订单信息
        List<UserPayment> userPaymentLists = userPaymentService.getProductOrderList1();

        //存储所有人的订单信息
        List<Payment> userRS = new ArrayList<>();

        //利用map的机制，计算出来其余用户的所有的购买商品的id  Map<用户id，商品ID拼接的字符串(1,2,3,4)>
        Map<Integer, String> map = new HashMap<>();


        //筛选出来订单中的商品的id
        for (int i = 0; i < userPaymentLists.size(); i++) {
            map.put(userPaymentLists.get(i).getUserId(),
                    map.containsKey(userPaymentLists.get(i).getUserId()) ?
                            map.get(userPaymentLists.get(i).getUserId()) + "," + userPaymentLists.get(i).getCommId() :
                            userPaymentLists.get(i).getCommId() + "");
        }

        //开始封装每个人的数据
        for (Integer key : map.keySet()) {

            //new出来一个新的个人的对象，后面要塞到list中
            Payment userR2 = new Payment();

            //把其他每个人购买的商品的id 分割成数组
            String[] split = map.get(key).split(",");

            //转换成int数组 进行存储，方便后期计算
            Long[] ints1 = new Long[split.length];
            for (int i = 0; i < split.length; i++) {
                ints1[i] = Long.valueOf(Integer.valueOf(split[i]));
            }
            //用户id 就是key
            userR2.setUserId(key);
            //用户购买的商品id的数组
            userR2.setProductIds(ints1);
            //塞到list中
            userRS.add(userR2);
        }

        //二值化 处理数据
        List<Payment> userRList = Recommend.jisuan(userR, userRS);
        //过滤处理
        String sqlId = Recommend.chuli(userRList, userR);

        System.out.println("推荐的商品：");
        //通过拿到的拼接的被推荐商品的id，去查数据库
        List<Commodity> productList = commodityService.getProductList(sqlId);
        //最终拿到被推荐商品的信息
        for (int i = 0; i < productList.size(); i++) {
            System.out.println(productList.get(i).toString());
        }
        return productList;

    }

}
